var htmlWebpackPlugin = require("html-webpack-plugin");

var path = require("path"); //node 自带的系统模块

// 项目根路径
var ProjectRoot = path.resolve(__dirname, "../");

// 源代码路径
var appPath = path.resolve(ProjectRoot, "./src");

// fs模块

var pagesPath = path.resolve(appPath, "./pages");

var fs = require("fs");

var chunks = {
  common: [path.resolve(appPath, "./common/index.js"), "jquery", "bootstrap"],
}; //所有的入口 chunk
var htmls = []; //所有的页面
// 同步读取文件夹

/**
 * 只要添加多一个页面，就要重启一起服务器
 * 
 */
function getAllChunk() {
  var files = fs.readdirSync(pagesPath, "utf8");
  files.forEach(function(item) {
    var chunkPath = path.resolve(pagesPath, "./" + item);
    // 同步读取文件或者文件夹的信息
    var state = fs.statSync(chunkPath);
    // 判断是不是一个文件夹
    if (state.isDirectory()) {
      var chunkFiles = fs.readdirSync(path.resolve(chunkPath));
      if (chunkFiles.length) {
        // 判断是否右入口文件
        console.log(chunkFiles.indexOf("index.js"));
        if (
          chunkFiles.indexOf("index.js") >= 0 &&
          chunkFiles.indexOf("index.pug") >= 0
        ) {
          chunks[item] = path.resolve(chunkPath, "./index.js");
          htmls.push(
            new htmlWebpackPlugin({
              template: path.resolve(chunkPath, "./index.pug"),
              filename: item + ".html",
              chunks: [item, "common"]
            })
          );
        }
      }
    }
  });
}
getAllChunk();

module.exports = {
  // 开发环境的路径
  devPublicPath: "http://localhost:3000/",
  // 生产环境的路径
  productionPublicPath: "http://luyishiqi.gotoip1.com/",

  // 打包路径
  buildPath: path.resolve(ProjectRoot, "./打包代码"),
  // 入口（chunk）的配置
  chunks:chunks,
  htmls:htmls
};
